import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;

public class Hangman {
    static int lives;
    static ArrayList<String> Triedletters = new ArrayList<String>();
    static String SecretWord;
    static boolean IsPlaying = true;
    static int guesses = 0;

    //Generate Secret Word
    public static String AskForWord() {
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("WHAT IS THE SECRET WORD???");
            String word = in.readLine().toLowerCase();
            for (int i = 100; i > 0; i--)
                System.out.println();
            return (word);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return ("Hangman");
    }

    //Print Letters you have and lives
    public static void PrintLivesAndTriedLetters() {
        if (guesses >= 1)
            System.out.println("The body parts you have are: Head");
        if (guesses >= 2)
            System.out.println("Body");
        if (guesses >= 3)
            System.out.println("Left Arm");
        if (guesses >= 4)
            System.out.println("Right Arm");
        if (guesses >= 5)
            System.out.println("Left Leg");
        if (guesses >= 6)
            System.out.println("Right Leg");
        System.out.println("(You currently have " + (6 - guesses) + " incorrect guesses left.)");

        Collections.sort(Triedletters);
        for (String letter : Triedletters)
            System.out.print(letter + " ");
        System.out.print("");

    }

    //Compare each letter in secret word to letters that are guessed
    public static void PrintWordSoFar() {
        for (int x = 0; x < SecretWord.length(); x++) {
            String Letter = SecretWord.substring(x);
            boolean found = false;
            for (String t : Triedletters) {
                if (Letter.startsWith(t)) {
                    System.out.println(t);
                    found = true;
                }
            }
            if (!found) {
                System.out.print("_ ");
            }
        }
    }

    //Get a letter guess from user
    public static void GetLetter() {
        System.out.println("Guess a letter...................please.");
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            String letter = in.readLine().toLowerCase();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //Check if you've won or lost
    //If won, return 1, lost all lives, -1, neither, 0
    public static int GameStatus() {
        boolean Solved = true;
        for (int x = 0; x < SecretWord.length(); x++) {
            String letter = SecretWord.substring(x);
            boolean found = false;
            for (String t : Triedletters) {
                if (letter.startsWith(t))
                    found = true;
            }
            if (!found)
                Solved = false;
            if (Solved)
                return 1;
            else if (!Solved && guesses >= 6)
                return -1;
            else
                return 0;
        }
        return 1;
    }

    public static void main(String[] args) {
        lives = 6;
        SecretWord = AskForWord();
        while (IsPlaying = true) {
            PrintLivesAndTriedLetters();
            GetLetter();
            PrintWordSoFar();
            GameStatus();
            if (GameStatus() == 1) {
                System.out.println("You win!!");
                IsPlaying = false;
            }
            if (GameStatus() == -1) {
                System.out.println("YOU LOSE!!! HA! LOSER :P");
                IsPlaying = false;
            }
        }
    }
}
